package com.caration.shell.workspace2D;

import java.util.ArrayList;

import com.caration.shell.cr3d.SharedData;

import android.content.Context;
import android.graphics.Rect;
import android.os.Handler;
import android.os.IBinder;
import android.util.DisplayMetrics;
import android.view.MotionEvent;
import android.view.View;
import android.view.WindowManager;
import android.view.inputmethod.InputMethodManager;

public class DragController {
	public static int DRAG_ACTION_MOVE = 0;
	public static int DRAG_ACTION_COPY = 1;
	private Workspace mWorkspace;
	private ak B;
	private Object C;
	private int D;
	private int E;
	private boolean F;
	private com.tsf.shell.workspace3D.f.a G;
	private boolean H;
	private Context mContext;
	private Handler mHandler;
	private Rect mRectTemp;
	private final int mCoordinatesTemp[] = new int[2];
	private boolean mDragging;
	private float h;
	private float i;
	private DisplayMetrics mDisplay;
	private View k;
	private float l;
	private float m;
	private h n;
	private Object o;
	private i p;
	private ArrayList mDropTargets;
	private com.tsf.shell.workspace2D.e r;
	/** The window token used as the parent for the DragView. */
    private IBinder mWindowToken;
    /** The view that will be scrolled when dragging to the left and right edges of the screen. */
    private View mScrollView;
	private View mMoveTarget;
	private g v;
	private f w;
	private a x;
	private MotionEvent mMotionEvent;
	private InputMethodManager mInputManager;

	public DragController(Context context) {
		mRectTemp = new Rect();
		mDisplay = new DisplayMetrics();
		mDropTargets = new ArrayList();
		w = new f(this);
		F = false;
		H = false;
		C = this;
		mContext = context;
		mHandler = new Handler();
		B = fm.h();
	}

	private static int validateNum(int index, int max) {
		return Math.min(Math.max(index, 0), max);
	}

//	static g a(b b1) {
//		return b1.v;
//	}
//
//	static void a(b b1, boolean flag) {
//		b1.H = flag;
//	}

	private boolean a(float f1, float f2) {
		boolean flag;
		int ai[] = mCoordinatesTemp;
		int l1 = (int) f1;
		int j1 = (int) f2;
		Rect rect = mRectTemp;
		ArrayList arraylist = mDropTargets;
		for (int j2 = -1 + arraylist.size(); j2 >= 0;) {
			flag = (j) arraylist.get(j2);
			flag.getHitRect(rect);
			flag.getLocationOnScreen(ai);
			rect.offset(ai[0] - flag.getLeft(), ai[1] - flag.getTop());
			if (!rect.contains(l1, j1)) {
				j2--;
			} else {
				ai[0] = l1 - ai[0];
				ai[1] = j1 - ai[1];
				break label0;
			}
		}
		flag = null;
		//

		if (flag == null) {
			flag = false;
		} else {
			h _tmp = n;
			float _tmp1 = l;
			float _tmp2 = m;
			i _tmp3 = p;
			Object _tmp4 = o;
			h _tmp5 = n;
			int k1 = ai[0];
			int k2 = ai[1];
			int i2 = (int) l;
			int i1 = (int) m;
			i _tmp6 = p;
			Object _tmp7 = o;
			if (!flag.a(k1, k2, i2, i1)) {
				H = true;
				n.a((View) flag, false);
				flag = true;
			} else {
				F = true;
				G = B.h();
				if (!x.ao())
					mWorkspace.a(D, E);
				h h1 = n;
				float _tmp8 = l;
				float _tmp9 = m;
				i _tmp10 = p;
				Object _tmp11 = o;
				flag.a(h1);
				n.a((View) flag, true);
				flag = true;
			}
		}
		return flag;
	}

//	static a b(b b1) {
//		return b1.x;
//	}

	static MotionEvent c(b b1) {
		return b1.y;
	}

	private void c(MotionEvent motionevent) {
		if (mDragging) {
			mDragging = false;
			MotionEvent event = MotionEvent.obtain(motionevent);
			if (k != null)
				fm.h().A();
			SharedData.getGLHandler().queueEvent(new Runnable() {
				
				@Override
				public void run() {
					  if (F && (G != B.h()))
					    {
					      B.a(G);
					      G=null;
					      F=false;
					      H=true;
					    }
					    if (H)
					    {
					      H=false;
					      B.p();
					    }
					    if (k != null&& x != null && x.an() != null)
					      x.an().d(this.b);
//					    if (r != null)
//					      r;
					  }
				}
			};);
		}
	}

	// static Object d(b b1) {
	// return b1.C;
	// }
	//
	// static boolean e(b b1) {
	// return b1.F;
	// }
	//
	// static com.tsf.shell.workspace3D.f.a f(b b1) {
	// return b1.G;
	// }
	//
	// static ak g(b b1) {
	// return b1.B;
	// }
	//
	// static void h(b b1) {
	// b1.G = null;
	// }
	//
	// static void i(b b1) {
	// b1.F = false;
	// }
	//
	// static boolean j(b b1) {
	// return b1.H;
	// }
	//
	// static View k(b b1) {
	// return b1.k;
	// }
	//
	// static com.tsf.shell.workspace2D.e l(b b1) {
	// return b1.r;
	// }

	public final void setWindowToken(IBinder ibinder) {
		mWindowToken = ibinder;
	}

	//a(v)
    /**
     * Sets the view that should handle move events.
     */
    void setMoveTarget(View view) {
        mMoveTarget = view;
    }    

	public final void a(View view, h h1, Object obj)
    {
        Object obj1;
        k = view;
        int ai[] = new int[2];
        view.getLocationOnScreen(ai);
        int i1 = ai[0];
        int j1 = ai[1] - com.censivn.C3DEngine.a.b();
        if(mInputManager == null)
            mInputManager = (InputMethodManager)mContext.getSystemService(Context.INPUT_METHOD_SERVICE);
        mInputManager.hideSoftInputFromWindow(mWindowToken, 0);
        if(r != null)
        {
            r;
        }
        l = h - (float)i1;
        m = i - (float)j1;
        mDragging = true;
        n = h1;
        o = obj;
        obj1 = fm.o().a(k);
        if(obj1 != null && ((com.tsf.shell.b.b) (obj1)).p != null && ((com.tsf.shell.b.b) (obj1)).p.b != null) goto _L2; else goto _L1
_L1:
        return;
_L2:
        c c1;
        ((com.tsf.shell.b.b) (obj1)).d();
        ((com.tsf.shell.b.b) (obj1)).n.setPressed(false);
        D = ((com.tsf.shell.b.b) (obj1)).e;
        E = ((com.tsf.shell.b.b) (obj1)).f;
        fm.o().a(((com.tsf.shell.b.b) (obj1)), 1);
        fm.h().z();
        x = ((com.tsf.shell.b.b) (obj1)).p.b;
        c1 = new c(this);
        obj1 = C;
        obj1;
        JVM INSTR monitorenter ;
        com.censivn.C3DEngine.a.a();
        com.censivn.C3DEngine.b.w.c(c1);
        C.wait();
          goto _L1
        Object obj2;
        obj2;
        throw obj2;
        obj2;
        ((InterruptedException) (obj2)).printStackTrace();
          goto _L1
    }

	// a
	public final void addTarget(DropTarget target) {
		mDropTargets.add(target);
	}

	//a(s)
	public final void setWorkspace(Workspace workspace) {
		mWorkspace = workspace;
	}

	public final boolean ignoreKeyEvent() {
		return mDragging;
	}

	public final boolean onInterceptTouchEvent(MotionEvent motionevent) {
		int action = motionevent.getAction();
		if (action == MotionEvent.ACTION_DOWN) {
			mMotionEvent = MotionEvent.obtain(motionevent);
			((WindowManager) mContext.getSystemService(Context.WINDOW_SERVICE))
					.getDefaultDisplay().getMetrics(mDisplay);
		}
		int k1 = validateNum((int) motionevent.getRawX(), mDisplay.widthPixels);
		int i1 = validateNum((int) motionevent.getRawY(), mDisplay.heightPixels);
		switch (action) {
		case MotionEvent.ACTION_MOVE:
			break;
		case MotionEvent.ACTION_DOWN:
			h = k1;
			i = i1;
			break;
		case MotionEvent.ACTION_UP:
		case MotionEvent.ACTION_CANCEL:
			F = false;
			G = null;
			if (mDragging)
				a(k1, i1);
			c(motionevent);
			break;
		}
		return mDragging;
	}

	 public boolean dispatchUnhandledMove(View focused, int direction) {
	        return mMoveTarget != null && mMoveTarget.dispatchUnhandledMove(focused, direction);
	    }

	//b(view)
	   /**
     * Set which view scrolls for touch events near the edge of the screen.
     */
    public void setScrollView(View v) {
		mScrollView = v;
	}

	// b
	public final void removeTarget(DropTarget target) {
		mDropTargets.remove(target);
	}

	public final boolean onTouchEvent(MotionEvent motionevent) {
		boolean flag = false;
		if (mDragging) {
			int i1 = validateNum((int) motionevent.getRawX(),
					mDisplay.widthPixels);
			int j1 = validateNum((int) motionevent.getRawY(),
					mDisplay.heightPixels);
			switch (motionevent.getAction()) {

			case MotionEvent.ACTION_DOWN:
				h = i1;
				i = j1;
				break;

			case MotionEvent.ACTION_UP:
			case MotionEvent.ACTION_CANCEL:
				F = false;
				G = null;
				mHandler.removeCallbacks(w);
				if (mDragging)
					validateNum(i1, j1);
				c(motionevent);
				break;

			case MotionEvent.ACTION_MOVE:
				if (mDragging && x != null && x.an() != null)
					x.an().a(mMotionEvent, motionevent);
				break;
			}
			flag = true;
		}
		return flag;
	}

}
